#! /usr/bin/env python
# -*- coding: utf-8 -*-

import demjson
import time

import monitor.check_os as check_os
import monitor.check_software as check_software
import utils.config_util as config_util
import utils.request_util as request_util


def check():
    json = {}
    # 获取系统资源利用情况
    check = config_util.get_config("system_utilization", "check").lower()
    if check == "true":
        system_utilization = check_os.get_system_utilization()
        if system_utilization != {}: json["system_utilization"] = system_utilization

    # 获取系统资源利用情况
    check = config_util.get_config("system_info", "check").lower()
    if check == "true":
        system_info = check_os.get_system_info()
        if system_info != {}: json["system_info"] = system_info

    # 软件信息
    check = config_util.get_config("software", "check").lower()
    if check == "true":
        software = check_software.get_software_info()
        if software != {}: json["software_info"] = software
    return demjson.encode(json)


def request(json):
    upload = config_util.get_config("upload", "upload").lower()
    if check == "true":
        method = config_util.get_config("upload","method").lower()
        url = config_util.get_config("upload","url")
        if method == "get":
            request_util.get(url,{"data":json})
        if method == "post":
            request_util.post_json(url,json)


if __name__ == '__main__':
    start = time.time()
    json = check()
    request(json)
    end = time.time()
    print(end - start)
    print(json)
